define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user_recycler_info/index' + location.search,
                    edit_url: 'user_recycler_info/edit',
                    multi_url: 'user_recycler_info/multi',
                    order_url: 'order/index',
                    appraise_url: 'order_appraise/index',
                    table: 'user_recycler_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.avatar', title: __('User.avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'poor_scale', title: __('Poor_scale'), operate: false},
                        {field: 'region_id', title: __('Region_id'), visible: false},
                        {field: 'region.name', title: __('Region.name'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'idcard', title: __('Idcard'), operate: 'LIKE'},
                        {field: 'positive', title: __('Positive'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'back', title: __('Back'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'audit_state', title: __('Audit_state'), searchList: {"-1":__('Audit_state -1'),"0":__('Audit_state 0'),"1":__('Audit_state 1')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: {
                                'click .btn-editone': Table.api.events.operate['click .btn-editone'],
                                'click .btn-delone': Table.api.events.operate['click .btn-delone'],
                                'click .btn-order': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var table = $(this).closest('table');
                                    var options = table.bootstrapTable('getOptions');
                                    var ids = row[options.pk];
                                    row = $.extend({}, row ? row : {}, {ids: ids});
                                    var url = options.extend.order_url + '/recycler_id/' + row.id;
                                    Layer.full(Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Order'), $(this).data() || {}));
                                },
                                'click .btn-appraise': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var table = $(this).closest('table');
                                    var options = table.bootstrapTable('getOptions');
                                    var ids = row[options.pk];
                                    row = $.extend({}, row ? row : {}, {ids: ids});
                                    var url = options.extend.appraise_url + '/recycler_id/' + row.id;
                                    Layer.full(Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Order'), $(this).data() || {}));
                                }
                            },
                            buttons: [
                                {
                                    name: 'order',
                                    text: __('Order'),
                                    icon: '',
                                    classname: 'btn btn-xs btn-info btn-order'
                                },
                                {
                                    name: 'appraise',
                                    text: __('Appraise'),
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-appraise'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            // Controller.api.bindevent();
            // Table.api.events.image('', 'http://www.baidu.com');
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});